package com.example.radiationarea.util;

import com.example.radiationarea.RadiationAreaMod;
import com.example.radiationarea.config.ModConfigs;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.ArrayList;
import java.util.List;

public class RadiationSuitHelper {
    
    // 内置支持的防辐射装备列表（包括Zombie Extansion模组的装备）
    private static final List<String> BUILT_IN_RADIATION_SUIT_ITEMS = new ArrayList<>();
    
    static {
        // 添加Zombie Extansion模组的防辐射装备
        BUILT_IN_RADIATION_SUIT_ITEMS.add("zombie_extansion:radiation_suit_helmet");
        BUILT_IN_RADIATION_SUIT_ITEMS.add("zombie_extansion:radiation_suit_chestplate");
        BUILT_IN_RADIATION_SUIT_ITEMS.add("zombie_extansion:radiation_suit_leggings");
        BUILT_IN_RADIATION_SUIT_ITEMS.add("zombie_extansion:radiation_suit_boots");
    }
    
    /**
     * 检查玩家是否穿戴了完整的防辐射装备
     * @param player 玩家
     * @return 如果穿戴了完整的防辐射装备则返回true，否则返回false
     */
    public static boolean isPlayerWearingFullRadiationSuit(Player player) {
        // 检查所有装备槽位
        return isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.HEAD)) &&
               isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.CHEST)) &&
               isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.LEGS)) &&
               isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.FEET));
    }
    
    /**
     * 检查指定物品是否为防辐射装备
     * @param stack 物品堆
     * @return 如果是防辐射装备则返回true，否则返回false
     */
    private static boolean isItemInSlotRadiationProtected(ItemStack stack) {
        if (stack.isEmpty()) {
            return false;
        }
        
        Item item = stack.getItem();
        ResourceLocation itemKey = ForgeRegistries.ITEMS.getKey(item);
        if (itemKey == null) {
            return false;
        }
        
        String itemIdentifier = itemKey.toString();
        
        // 首先检查内置支持的防辐射装备（包括Zombie Extansion）
        for (String builtInItem : BUILT_IN_RADIATION_SUIT_ITEMS) {
            if (builtInItem.equals(itemIdentifier)) {
                return true;
            }
        }
        
        // 然后检查配置文件中定义的防辐射装备
        for (String radiationSuitItem : ModConfigs.SERVER.radiationSuitItems.get()) {
            if (radiationSuitItem.equals(itemIdentifier)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * 检查玩家是否穿戴了任意防辐射装备（非全套）
     * @param player 玩家
     * @return 如果穿戴了任意防辐射装备则返回true，否则返回false
     */
    public static boolean isPlayerWearingAnyRadiationSuit(Player player) {
        return isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.HEAD)) ||
               isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.CHEST)) ||
               isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.LEGS)) ||
               isItemInSlotRadiationProtected(player.getItemBySlot(EquipmentSlot.FEET));
    }
}